Passed
Branch master (fbf0a6)
by Volodymyr
04:04
created

Component.extend.initialize   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
/*
2
 * Copyright (c) 2019. Volodymyr Hryvinskyi.  All rights reserved.
3
 * @author: <mailto:[email protected]>
4
 * @github: <https://github.com/hryvinskyi>
5
 */
6
7
define([
8
    'jquery',
9
    'ko',
10
    'uiComponent',
11
    './model/invisible-captcha'
12
], function ($, ko, Component, invisibleCaptcha) {
13
    'use strict';
14
15
    return Component.extend({
16
        defaults: {
17
            template: 'Hryvinskyi_InvisibleCaptcha/invisible-captcha',
18
            action: '',
19
            captchaId: ''
20
        },
21
        _initializedForms: [],
22
23
        /**
24
         * Initialization
25
         */
26
        initialize: function () {
27
            this._super();
28
            this._loadGoogleApi();
29
        },
30
31
        /**
32
         * Initialize Google ReCaptca Script
33
         *
34
         * @private
35
         */
36
        _loadGoogleApi: function () {
37
            var self = this;
38
39
            if (invisibleCaptcha.isApiLoad() === true) {
40
                $(window).trigger('recaptcha_api_ready_' + self.captchaId);
41
                return;
42
            }
43
44
            window.onloadCallbackGoogleRecapcha = function () {
45
                invisibleCaptcha.isApiLoaded(true);
46
                invisibleCaptcha.initializeForms.each(function (item) {
47
                    self._initializeTokenField(item.element, item.self);
48
                });
49
                $(window).trigger('recaptcha_api_ready_' + self.captchaId);
50
            };
51
52
53
            if(invisibleCaptcha.isApiLoaded() === true) {
54
                return;
55
            }
56
57
            require([
58
                '//www.google.com/recaptcha/api.js?onload=onloadCallbackGoogleRecapcha&render=' + self.siteKey
59
            ]);
60
61
            invisibleCaptcha.isApiLoad(true);
62
        },
63
64
        /**
65
         * Loads google API and triggers event, when loaded
66
         *
67
         * @private
68
         */
69
        _initializeTokenField: function (element, self) {
70
            if (invisibleCaptcha.initializedForms.indexOf(self.captchaId) === -1) {
71
                invisibleCaptcha.initializedForms.push(self.captchaId);
72
73
                var tokenField = $('<input type="hidden" name="hryvinskyi_invisible_token" />'),
74
                    siteKey = self.siteKey,
75
                    action = self.action;
76
77
                grecaptcha.ready(function () {
0 ignored issues
show
Bug introduced by
The variable grecaptcha seems to be never declared. If this is a global, consider adding a /** global: grecaptcha */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
78
                    grecaptcha
0 ignored issues
show
Bug introduced by
The variable grecaptcha seems to be never declared. If this is a global, consider adding a /** global: grecaptcha */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
79
                        .execute(siteKey, {action: action})
80
                        .then(function (token) {
81
                            tokenField.val(token);
82
                        });
83
                });
84
                tokenField.attr('data-action', action);
85
                $(element).append(tokenField);
86
            }
87
        },
88
89
        /**
90
         * Initialize recaptcha
91
         *
92
         * @param {Dom} element
93
         * @param {Object} self
94
         */
95
        initializeCaptcha: function (element, self) {
96
            if (invisibleCaptcha.isApiLoad() === true && invisibleCaptcha.isApiLoaded() !== true) {
97
                invisibleCaptcha.initializeForms.push({'element': element, self: self});
98
            } else if(invisibleCaptcha.isApiLoaded() === true) {
99
                self._initializeTokenField(element, self);
100
            } else {
101
                $(window).on('recaptcha_api_ready_' + self.captchaId, function () {
102
                    self._initializeTokenField(element, self);
103
                });
104
            }
105
        }
106
    });
107
});